package com.csx.springcloud.governance.sentinel.config;

import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreaker;
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.EventObserverRegistry;
/**
 * 收集Sentinel熔断状态的指标
 * @author cuisongxu
 * @date 2023/2/1 14:00
 */
public class SentinelDegradeState {

    /**
     * TODO 需要有一个合适的时机去触发调用
     */
    public static void registerDegradeStateChangeObserver() {

        EventObserverRegistry.getInstance().addStateChangeObserver("degradeState", (preState, curState, rule, tempValue) -> {
            // 熔断开启，拒绝所有请求
            if(CircuitBreaker.State.OPEN.equals(curState)) { // 熔断开启
                System.out.println("熔断开启");
            }else if(CircuitBreaker.State.HALF_OPEN.equals(curState)) { // 熔断半开
                System.out.println("熔断半开");
            } else { // 熔断关闭
                System.out.println("熔断关闭");
            }
        });
    }
}
